//
//  CSYTabBar.swift
//  CSSinaMicroBlog_Test
//
//  Created by 姚彦兆 on 15/11/11.
//  Copyright © 2015年 姚彦兆. All rights reserved.
//

import UIKit

class CSYTabBar: UITabBar {

    //设置布局
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        var index : CGFloat = 0
        
        let btn_W : CGFloat = self.bounds.width / 5
        
        let btn_H : CGFloat = self.bounds.height
        
        let btn_X : CGFloat = 0
        
        let btn_Y : CGFloat = 0
        
        let rect = CGRectMake(btn_X, btn_Y, btn_W, btn_H)
        
        for subView in subviews{

            if(subView.isKindOfClass(NSClassFromString("UITabBarButton")!)){
            
                subView.frame = CGRectOffset(rect, btn_W * index, 0)
            
                index += index == 1 ? 2 : 1
                
            }

        }
        
        composeBtn.frame = CGRectOffset(rect, btn_W * 2, 0)
        
        bringSubviewToFront(composeBtn)
        
    }
    
    //设置UI
    private func setUpUI(){
    
        addSubview(composeBtn)
        
    }
    
    //懒加载发布微博按钮
    lazy var composeBtn : UIButton = {
    
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Normal)
        
        button.sizeToFit()
        
        return button
    
    }()
    
    //构造方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUpUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        
        super.init(coder: aDecoder)
        
        setUpUI()
        
    }
    
    
    
    
    
}
